package com.gx.smart.construction.ui.common.fragment;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gx.smart.base.BaseAdapter;
import com.gx.smart.base.BaseMVVMFragmentV2;
import com.gx.smart.base.BaseViewModel;
import com.gx.smart.construction.ui.common.adapter.BaseAdapterWithDiff;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;


/**
 * 基础fragment
 */

public abstract class BaseMVVMRefreshFragmentV3<VM extends BaseViewModel> extends BaseMVVMFragmentV2<VM>
        implements OnRefreshListener, OnLoadMoreListener {

    public static final int PAGE_SIZE = 10;
    protected int currentPage = 1;
    private RefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private BaseAdapterWithDiff adapter;
    private boolean isBackRefresh = false;//返回刷新
    private boolean isBack = false;//返回刷新


    @Override
    public void onStart() {
        super.onStart();
        if (isBackRefresh && isBack) {
            refreshLayout.autoRefresh();
            isBack = false;
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        isBack = true;
    }


    /**
     * 使用viewBinding
     */
    protected void initView(RecyclerView recyclerView, BaseAdapterWithDiff adapter, RefreshLayout refreshLayout) {
        this.refreshLayout = refreshLayout;
        this.refreshLayout.setOnRefreshListener(this);
        this.refreshLayout.setOnLoadMoreListener(this);
        this.recyclerView = recyclerView;
        this.adapter = adapter;
        this.recyclerView.setAdapter(adapter);
    }


    protected void setBackRefresh() {
        isBackRefresh = true;
    }

    /**
     * 获取viewModel中的数据
     */
    protected void initObserver() {
        viewModel.error.observe(getViewLifecycleOwner(), result -> {
            if (result) {
                handleErrorResult();
            }
        });
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        currentPage = 1;
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        currentPage++;
    }

    /**
     * 处理请求返回后统一的分页和加载更多处理
     *
     * @param list
     */
    protected void handleResult(List list) {
        if (refreshLayout == null) {
            return;
        }

        if (currentPage == 1 || currentPage == 0) {
            refreshLayout.finishRefresh();
        }

        if (list.isEmpty()) {
            showNoDataView(true);
            refreshLayout.setEnableLoadMore(false);
            return;
        }
        if (list.size() < PAGE_SIZE) {
            refreshLayout.finishLoadMoreWithNoMoreData();
        } else {
            refreshLayout.finishLoadMore();
            refreshLayout.setEnableLoadMore(true);
        }
        showNoDataView(false);
        adapter.submitList(list);
    }


    private void handleErrorResult() {
        if (refreshLayout == null) {
            return;
        }
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        showNoDataView(true);
    }
}
